--[[ 婆娑罗1代,从FBNEO的武装飞鸟2脚本改装过来
  作用是显示坐标中心方块和十字线,提示能量数值
  
  By setycyas @2025-10-21
]]

myFBN = require('MyFBN')
myFBN.X_RESOLUTION, myFBN.Y_RESOLUTION = 336, 240

-- 这里设定基本参数
GAME = {
  -- 引用模块
  myFbn = myFBN,
  -- 地址
  -- 横坐标纵坐标内存地址
  X_ADDR_HIGH = 0xffb4+1,
  X_ADDR_LOW = 0xffb4,
  Y_ADDR_HIGH = 0xffb8+1,
  Y_ADDR_LOW = 0xffb8,
  -- 能量地址,最大106
  POWER_ADDR = 0x2EE6,
  -- 坐标最终修正
  X_OFFSET = 5,
  Y_OFFSET = 3,
  -- 能量分界线定义,高于该数值线颜色改变提示
  POWER_LINE = 76,
  -- 自己中心方块的半长半高,单位为原像素
  BOX_HALF_WIDTH = 2,
  BOX_HALF_HEIGHT = 2,
  -- 中心方块颜色(RGB-Alpha格式)
  BOX_BORDER_COLOR = 0x0000ff88,
  BOX_COLOR = 0x0000cc88,
  -- 十字线颜色
  LINE_COLOR = 0x00ffff88,
  LINE_POWER_COLOR = 0xff000088,
  -- 提示文字颜色
  TEXT_BORDER_COLOR = 0x000000FF,
  TEXT_COLOR = 0x00ff00FF,
}

-- 坐标变换,从内存坐标变为屏幕显示位置,返回原游戏像素,
function GAME.getX_u8()
  local result = -1
  local xlow = memory.readbyte(GAME.X_ADDR_LOW)
  local xhigh = memory.readbyte(GAME.X_ADDR_HIGH)
  -- 高位过大,位置不对
  if (xhigh > 1) then
    return -1
  end
  if (xhigh == 1) then
    result = 0x100+xlow
  else
    result = xlow
  end
  return result+GAME.X_OFFSET
end
function GAME.getY_u8()
  local result = -1
  local ylow = memory.readbyte(GAME.Y_ADDR_LOW)
  local yhigh = memory.readbyte(GAME.Y_ADDR_HIGH)
  -- 高位过大,位置不对
  if (yhigh > 1) then
    return -1
  end
  if (yhigh == 1) then
    result = 0x100+ylow
  else
    result = ylow
  end
  return result+GAME.Y_OFFSET
end
-- 获取坐标
function GAME.getXY()
  return GAME.getX_u8(), GAME.getY_u8()
end

-- 画辅助线
function GAME.showAssist()
  gui.box(0, 119, 10, 121, "BLUE", "BLACK")
end

-- 显示火力等级
function GAME.showPow()
  local power, color
  power = memory.readbyte(GAME.POWER_ADDR)
  --color = 0x00dd00AA    
  --gui.text(10, 0, power, color)
  emu.message(power)
end

-- 画中心十字线
function GAME.showCenter()
  local x, y, power, lineColor
  -- 获取数据
  x, y = GAME.getXY()
  power = memory.readbyte(GAME.POWER_ADDR)
  if (power > GAME.POWER_LINE) then
    lineColor = GAME.LINE_POWER_COLOR
  else
    lineColor = GAME.LINE_COLOR
  end
  -- 画线
  print(x)
  print(y)
  myFBN.xLine(x, 2, lineColor)
  myFBN.yLine(y, 2, lineColor)
  -- 画中心
  myFBN.centerBox(x, y, 
    GAME.BOX_HALF_WIDTH, GAME.BOX_HALF_HEIGHT, 
    GAME.BOX_COLOR, GAME.BOX_BORDER_COLOR
  )
end

-- 每帧运行函数
function GAME.Main()
  --GAME.showPow()
  GAME.showAssist()
  GAME.showCenter()
end

--[[运行]]
-- 设定每帧运行
myFBN.runPerFrame(GAME.Main)
-- 导出到全局变量
_G.game = GAME

return GAME
